import {observable, action} from "mobx";
import * as Apis from "../../apis/receiveRegistApi";
import * as allFun from "../../../workflowForm/util/forwardPage";
import { Form } from 'weaver-mobile-page';
import { Bubble,Tools } from "weaver-mobile";

const getLabel = Tools.getLabel;

class ReceiveRegistStore{

  @observable imgList=[];

  @observable form = new Form.Store({ route: '/receiveRgist' });
  @observable workflowId = "";
  @observable selectedId = [];
  @observable document_identifier = "";
  @observable type = "0"
  @observable fileInfo={
    Document_Text:[],
    Document_Attachments:[],
  }
  @observable fileId={
    Document_Text:"",
    Document_Attachments:"",
  }

  @action.bound 
  getform(){
    const params = { 
      menuType: "receiptRegistration",
    }
    Apis.getCondition(params).then(res => {
      this.form.initForm(res.condition, '/receiveRgist')
    });
  }
  
  //登记保存
  @action.bound  
  registSave(CreatFlow){
    const result = this.form.doValidate();
    if (!result.pass) {
      Bubble.error(result.message, 3, {}, 2);
      return ;
    }
    if(this.fileId.Document_Text === ""){
      Bubble.error(getLabel('502017','正文文件没有上传') , 3, {}, 2);
      return ;
    }
    let formData = this.form.formParams;   
    formData.Document_Attachments = this.fileId.Document_Attachments;
    formData.Document_Text = this.fileId.Document_Text;
    const params = {...formData , receiveDocType:this.type,receiveStatus:"0"}
    Apis.saveRegistrationFormData(params).then(data=>{
      if(data.api_status){
        const dataParams = {
          ids : data.message ,
          createWfid : data.workflowId,
        }
        Apis.getRequstId(dataParams).then(res=>{
          if(res.status){
            allFun.forwardViewForm(({requestid:res.requestid,f_weaver_belongto_userid: "1"}, (arg) =>{

            },"wfCenter"))
          }         
        })
      }
    })
  }

  //来文转收文
  @action.bound 
  receiveToOther(){
    this.registSave(true)
   
  }
  
  @action.bound 
  getFileData(condition){
    condition[0].items.forEach(element => {
      if(element.domkey[0] === "Document_Text"){
        this.fileInfo.Document_Text = element.datas;
        let idArr = []
        element.datas.map(item=>{
          idArr.push(item.fileid)
        })
        this.fileId.Document_Text = idArr.join(",");
      }
    });
  }

  @action.bound 
  fileUploadChange(ids, datas,type){
    const id =ids.join(",");
    if(type === "Text"){
      this.fileInfo.Document_Text = datas;
      this.fileId.Document_Text = id;
    }else{
      this.fileInfo.Document_Attachments = datas;
      this.fileId.Document_Attachments = id;
    }
  }
  

}
export default ReceiveRegistStore;